<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingDeviceData;
use App\Models\DedustingDevicePoint;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use Dcat\Admin\Widgets\Card;

class HistoryController extends Controller
{
    public function index(Request $request, Content $content, $id = 0, $points = 0)
    {   
        if ($points) {
            $checkedPoints = json_decode($points, true);
        } else {
            $checkedPoints = [];
        }

        return $content
            ->header('历史数据')
            ->description('')
            ->body($this->historyView($id, $checkedPoints, $request));
    }

    protected function historyView($deviceId, $checkedPoints, $request){
        $data['deviceId'] = $deviceId;

        $param = $request->all();
 
        $pointNav = [];
        $points = DedustingDevicePoint::POINTS;
        foreach ($points as $pointAlias => $pointName) {
            $onePointNav['name'] = $pointName;
            $onePointNav['active'] = in_array($pointAlias, $checkedPoints) ? 'active' : '';
            $onePointNav['alias'] = $pointAlias;
            array_push($pointNav, $onePointNav);
        }
        $data['pointNav'] = $pointNav;

        $data['points'] = $points;

        $data['url'] = $request->url();
        $data['checkeds'] = json_encode($checkedPoints);

        $grid = $this->historyGrid($deviceId, $checkedPoints, $points);
        $data['grid'] = $grid;
        
        $lineData = $this->historyLine($deviceId, $checkedPoints, $points, $request);
        $data = array_merge($data, $lineData);

        $card = Card::make(view('admin.Dedusting.history', $data));
        $card->padding('0');
        return $card;
    }

    protected function historyGrid($deviceId, $fields, $aliasNames){
        return Grid::make(new DedustingDeviceData(), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();

            $grid->showFilter();
            $grid->showFilterButton();
            $grid->filter(function($filter){
                // 展开过滤器
                $filter->panel();
                $filter->between('created_time', '上报时间')->datetime()->toTimestamp();
            });
            $grid->disablePagination();
        });
    }

    protected function historyLine($deviceId, $checkedPoints, $points, $request){
        $xData = [];
        $legend = [];
        $series = [];

        $param = $request->all();
        if (isset($param['created_time'])) {
            $start = strtotime($param['created_time']['start']);
            $end = strtotime($param['created_time']['end']);
        } else {
            $start = strtotime("-1 day");
            $end = time();
        }

        if (isset($param['per_page'])) {
            $size = $param['per_page'];
        } else {
            $size = 20;
        }
        if (isset($param['page'])) {
            $page = $param['page'];
        } else {
            $page = 1;
        }

        $d = DedustingDeviceData::getJsonDataList($deviceId, $checkedPoints, $start, $end);

        foreach ($d as $oneD) {
            $oneRecord = json_decode( json_encode( $oneD ),true);
            $xData[] = date("Y/m/d H:i:s", $oneRecord['created_time']);
            foreach ($checkedPoints as $oneCheckedPoints) {
                if (isset($oneRecord[$oneCheckedPoints])) {
                    $$oneCheckedPoints[] = $oneRecord[$oneCheckedPoints];
                } else {
                    $$oneCheckedPoints[] = 0;
                }
            }
        }

        foreach ($checkedPoints as $oneCheckedPoints) {
            $legend[] = $points[$oneCheckedPoints];
            $oneSeries['name'] = $points[$oneCheckedPoints];
            $oneSeries['type'] = 'line';
            $oneSeries['data'] = isset($$oneCheckedPoints) ? $$oneCheckedPoints : [];
            array_push($series, $oneSeries);
        }

        $data['xData'] = json_encode($xData);
        $data['series'] = json_encode($series);
        $data['legend'] = json_encode($legend);
        return $data;
    }
}